import Vue from 'vue'
import Router from 'vue-router'
import axios from 'axios'
import store from '@/store'; 
import NotFound from '@/components/NotFound'
import Home from '@/components/Home'
import Login from '@/components/Login'
import register from '@/components/register'
import imageManage from '@/components/ImageManagement'
import ProfessorManage from '@/components/ProfessorManage'
import ContestantManage from '@/components/ContestantManage'
import ProjectUpload from '@/components/ProjectUpload'
import PersonCenter from '@/components/PersonCenter'
import LoginByPhone from '@/components/LoginByPhone'
import FirstScoring from '@/components/FirstScoring'
import DistributeFirst from '@/components/DistributeFirst'
import FirstScoreProgress from '@/components/FirstScoreProgress'
import ScoreInquiry from '@/components/ScoreInquiry'
import DistributionFinal from '@/components/DistributionFinal'
import FinalScoring from '@/components/FinalScoring'
import FinalScoringProgress from '@/components/FinalScoringProgress'
import Prize from '@/components/Prize'
import AnnouncementManage from '@/components/AnnouncementManage'
import CriterionManage from '@/components/CriterionManage'
import CriterionView from '@/components/CriterionView'

axios.defaults.withCredentials = true; // 全局启用携带凭证
Vue.use(Router)
const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/login',
      name: Login,
      component: Login,
      meta: {
        auths:['']
      }
    },
    {
      path: '/loginByPhone',
      name: LoginByPhone,
      component: LoginByPhone,
      meta: {
        auths:['']
      }
    },
    {
      path: '/firstscoring',
      name: 'firstscoring',
      component: FirstScoring,
      meta: {
        auths:['2','3','4']
      }
    }, 
    {
      path: '/criterionView',
      name: 'criterionView',
      component: CriterionView,
      meta: {
        auths:['2','3','4','5']
      }
    }, 
    {
      path: '/criterionManage',
      name: 'criterionManage',
      component: CriterionManage,
      meta: {
        auths:['1']
      }
    },
    {
      path: '/finalscoring',
      name: 'FinalScoring',
      component: FinalScoring,
      meta: {
        auths:['2','3','4']
      }
    },
    {
      path: '/distributefinal',
      name: 'distributefinal',
      component: DistributionFinal,
      meta: {
        auths:['1']
      }
    },
    {
      path: '/AnnouncementManage',
      name: 'AnnouncementManage',
      component: AnnouncementManage,
      meta: {
        auths:['1']
      }
    },
    {
      path: '/prize',
      name: 'prize',
      component: Prize,
      meta: {
        auths:['1']
      }
    },
    {
      path: '/distributefirst',
      name: 'DistributeFirst',
      component: DistributeFirst,
      meta: {
        auths:['1']
      }
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      meta: {
        auths:['1','2','3','4','5']
      }
    },
    {
      path: '/firstscoreprogress',
      name: 'FirstScoreProgress',
      component: FirstScoreProgress,
      meta: {
        auths:['1']
      }
    },
    {
      path: '/finalscoringprogress',
      name: 'FinalScoringProgress',
      component: FinalScoringProgress,
      meta: {
        auths:['1']
      }
    },
    {
      path: '/personcenter',
      name: 'personcenter',
      component: PersonCenter,
      meta: {
        auths:['1','2','3','4','5']
      }
    },
    {
      path: '/projectupload',
      name: 'projectupload',
      component: ProjectUpload,
      meta: {
        auths:['5']
      }
    },

    {
      path: '/professorManage',
      name: 'professorManage',
      component: ProfessorManage,
      meta: {
        auths:['1']
      }
    },
    {
      path: '/contestants',
      name: 'contestants',
      component: ContestantManage,
      meta: {
        auths:['1']
      }
    },
    {
      path: '/register',
      name: 'register',
      component: register,
      meta: {
        auths:['']
      }
    },
    {
      path: '/quryscore',
      name: 'quryscore',
      component: ScoreInquiry,
      meta: {
        auths:['5']
      }
    },
    {
      path: '/imageManage',
      name: 'imageManage',
      component: imageManage,
      meta: {
        auths:['1']
      }
    }, {
      path: '/404',
      name: 'NotFound',
      component: NotFound,
      meta: {
        auths:['1','2','3','4','5']
      }
    },
    {
    path: '*',
    redirect: '/404'
  }
  ]
})
var count = 0;

router.beforeEach(async (to, from, next) => {
  const token = window.token || localStorage.getItem('token');

  axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
  if (to.path === '/login'||to.path ==='/register'||to.path ==='/loginByPhone') {
    next();
  } else {
    if (!token || token === '') {
      console.log('null')
      next({ path: '/login' });

    } else {
      const tokennew = token.replace(/^"(.*)"$/, '$1');
      console.log(tokennew)
      const headers = {
        'Content-Type': 'application/json', 
        Authorization: `Bearer ${tokennew}`,
      }
       axios.get('http://localhost:8080/check_token',headers
      ).then(res => {
        if (res.data.code == 1) {
          store.commit('SET_USER', res.data.data);
          console.log(store.userdata);
          var roleid = res.data.data.roleId;
          // console.log(to.meta.auths.includes(role));
          // next();
          if (to.meta.auths.includes(roleid)) {
            next();
          } else {
            next({path:'/404'})
          }
          
        } else {
          console.log("NOT_LOGIN")
          localStorage.removeItem('token');
          next({path:'/login'})
        }
      })
    }
  }
  
})
export default router;
